package com.doit.day01;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
//Iterator<Hero> 这个泛型就是返回值的类型

/**
 * 读取文件中的数据
 * 一次性读取一行，将这一行的信息解析，封装成一个Hero
 *
 * 回头我想要读取这个数据，返回hero对象的时候
 * 调用hasnext next方法   拿什么调用  对象  new HeroIterator ==> 调用构造方法
 *
 * while(it.hasNext()){
 *     it.next() ==>对象
 *
 * }
 *
 */

public class HeroIterator implements Iterator<ArrayList<Hero>> {
    private  BufferedReader br;
    private String line;
    private PreparedStatement pps;
    private ArrayList<Hero> list;

    public HeroIterator()  {
        try {
            br = new BufferedReader(new FileReader("data/hero.txt"));

            /*Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
            pps = conn.prepareStatement("insert into hero values (?,?,?,?,?)");*/
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public boolean hasNext() {
        list =  new ArrayList<Hero>();
        //打开文件的代码
        try {
            for (int i = 0; i < 2; i++) {
                line = br.readLine();
                if(line !=null){
                    //1,张飞_18::40==100
                    String[] arr = line.split(",");
                    //张飞_18::40==100
                    String[] arr1 = arr[1].split("_");
                    //18::40==100
                    String[] arr2 = arr1[1].split("::");
                    //40==100
                    String[] arr3 = arr2[1].split("==");
                    list.add(new Hero(Integer.parseInt(arr[0]),arr1[0],Integer.parseInt(arr2[0]),Double.parseDouble(arr3[0]),Double.parseDouble(arr3[1])));
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list.size() >= 1;
    }

    @Override
    public ArrayList<Hero> next() {
        return list;
    }
}
